<?php

declare(strict_types=1);

namespace N98\Magento\Command\Cache;

use Exception;
use Mage;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
 * Clean cache command
 *
 * @package N98\Magento\Command\Cache
 */
class CleanCommand extends AbstractCacheCommand
{
    protected function configure(): void
    {
        $this
            ->setName('cache:clean')
            ->addArgument('type', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Cache type code like "config"')
            ->addOption(
                'reinit',
                null,
                InputOption::VALUE_NONE,
                'Reinitialise the config cache after cleaning',
            )
            ->addOption(
                'no-reinit',
                null,
                InputOption::VALUE_NONE,
                "Don't reinitialise the config cache after flushing",
            )
            ->setDescription('Clean magento cache')
        ;
    }

    public function getHelp(): string
    {
        return <<<HELP
Cleans expired cache entries.

If you would like to clean only one cache type use like:

   $ n98-magerun.phar cache:clean full_page

If you would like to clean multiple cache types at once use like:

   $ n98-magerun.phar cache:clean full_page block_html

If you would like to remove all cache entries use `cache:flush`

Options:
    --reinit Reinitialise the config cache after cleaning (Default)
    --no-reinit Don't reinitialise the config cache after cleaning
HELP;
    }


    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $noReinitOption = $input->getOption('no-reinit');
        if (!$noReinitOption) {
            $this->banUseCache();
        }

        $this->detectMagento($output);
        if (!$this->initMagento(true)) {
            return Command::INVALID;
        }

        try {
            Mage::app()->loadAreaPart('adminhtml', 'events');
        } catch (Exception $exception) {
            $output->writeln('<error>' . $exception->getMessage() . '</error>');
        }

        $allTypes = Mage::app()->getCacheInstance()->getTypes();
        /** @var string[] $typesToClean */
        $typesToClean = $input->getArgument('type');
        $this->validateCacheCodes($typesToClean);
        $typeKeys = array_keys($allTypes);

        foreach ($typeKeys as $typeKey) {
            if (is_countable($typesToClean) ? count($typesToClean) : in_array($typeKey, $typesToClean)) {
                Mage::app()->getCacheInstance()->cleanType($typeKey);
                Mage::dispatchEvent('adminhtml_cache_refresh_type', ['type' => $typeKey]);
                $output->writeln('<info>Cache <comment>' . $typeKey . '</comment> cleaned</info>');
            }
        }

        if (!$noReinitOption) {
            $this->reinitCache();
        }

        return Command::SUCCESS;
    }
}
